const skCommon = {
  // 获取今天周几
  getWeekDate(time = new Date()){
    let weeks = new Array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六");
    let date = new Date(time);
    let day = date.getDay();
    return weeks[day];
  },
  // 获取当前时间
  getDate(status,tomorrow){
    let d = new Date();
    // 是否获取明天的日期
    if (tomorrow) {
      d.setTime(d.getTime()+24*60*60*1000);
    }
    let Y = d.getFullYear();
    let M = d.getMonth() + 1;
    let D = d.getDate();
    let H = d.getHours();
    let m = d.getMinutes();
    M = M <= 9 ? '0' + M:M;
    D = D <= 9 ? '0' + D:D;
    H = H <= 9 ? '0' + H:H;
    m = m <= 9 ? '0' + m:m;
    if (status) {
      return `${Y}-${M}-${D} ${H}:${m}`;
    }else{
      return `${Y}-${M}-${D}`
    }
  },
  // 获取指定月份天数
  // target 2023-03
  getMonthDay(target) {
    let date = new Date(target + "-01"); // 移动端使用格式要转成： "2018/10/01"
    date.setMonth(date.getMonth() + 1); // 先设置为下个月
    date.setDate(0); // 再置0，变成当前月最后一天
    return date.getDate(); // 当前月最后一天即当前月拥有的天数
  },
  //增加N天
  getDateAdd(day, dateStr = '') {
    // 获取日期
    var timestamp = Date.parse(new Date());
    if(dateStr != ''){
      timestamp = Date.parse(new Date(dateStr));
    }
    timestamp = timestamp /1000;
    timestamp += 86400 * day;//加一天
    var newTime = this.formatDate(new Date(timestamp * 1000), 'yyyy-MM-dd');
    return newTime;
  },
  // 转换数字并截取小数点后两位
  transformNum(num){
    return Number(num).toFixed(2)
  },
  // 表头日期
  tableHeaderDate({dateType,startDate,i,endDate}) {
    let str = "";
    switch (dateType) {
      case "week":
        str = startDate.substring(5, 12) + ' ' + this.getWeekDate(startDate)
        break;
      case "month":
        let subStartDate = startDate.substring(5, 12);
        if (endDate) {
          str =  subStartDate + '~' +endDate.substring(5, 12) + ' 第' +`${i+ 1}`+'周';
        }else{
          str =  subStartDate
        }
        break;
      default:
        str = startDate.substring(0, 7)
        break;
    }
    return str
  },
  // 日期格式化
  formatDate (date, fmt) {
    if (date === "" || date === null || date === undefined) {
      return null;
    }
    if (fmt === "" || fmt === null || fmt === undefined) {
      fmt = "yyyy-MM";
    }
    date = new Date(date);
    var opt = {
			'y+': date.getFullYear().toString(), // 年
			'Y+': date.getFullYear().toString(), // 年
      "M+": date.getMonth() + 1, // 月份
      "d+": date.getDate(), // 日
      "h+": date.getHours(), // 小时
      "m+": date.getMinutes(), // 分
      "s+": date.getSeconds(), // 秒
      "q+": Math.floor((date.getMonth() + 3) / 3), // 季度
      S: date.getMilliseconds(), // 毫秒
    };
    let ret;
    for (var k in opt) {
      ret = new RegExp('(' + k + ')').exec(fmt)
			if (ret) {
				fmt = fmt.replace(
					ret[1],
					ret[1].length == 1 ? opt[k] : opt[k].toString().padStart(ret[1].length, '0')
				)
			}
    }
    return fmt;
  },
  // 获取日期范围内月数
  // startDate 2022-01-26
  // endDate 2022-04-26
  // 返回[2022-01, 2022-02, 2022-03, 2022-04]
  getMonths(start, end, formatStr = 'yyyy-MM'){
    var result = []
    var s = start.split('-')
    var e = end.split('-')
    var min = new Date()
    var max = new Date()
    min.setFullYear(s[0], s[1] * 1 - 1, 1) //开始日期

    max.setFullYear(e[0], e[1] * 1 - 1, 1) //结束日期
    var curr = min
    while (curr <= max) {
      var month = curr.getMonth()
      result.push(this.formatDate(curr, formatStr))
      curr.setMonth(month + 1)
    }
    return result
  },
  // 获取日期范围内每个日期
  // startDate 2022-03-26
  // endDate 2022-04-26
  // 返回：[2022-03-26, 2022-03-27, 2022-03-28]
  getCurrentWeekDays(formatStr = 'MM-dd') {
    // 获取本周的第一天和最后一天
    var currentDay = new Date();
    let weekday = currentDay.getDay();
    if (weekday == 0) {
      weekday = 7
    }
    var first  = this.formatDate(new Date(currentDay.getFullYear(), currentDay.getMonth(), currentDay.getDate() + 1 - weekday), 'yyyy-MM-dd');
    var end  = this.formatDate(new Date(currentDay.getFullYear(), currentDay.getMonth(), currentDay.getDate() + 7 - weekday), 'yyyy-MM-dd');
    // var end  = this.formatDate(new Date(), 'yyyy-MM-dd');
    return this.getDays(first, end, formatStr)
  },
  // 获取日期范围内每个日期
  // startDate 2022-03-26
  // endDate 2022-04-26
  // 返回：[2022-03-26, 2022-03-27, 2022-03-28]
  getCurrentMonthDays(formatStr = 'MM-dd') {
    // 获取当月的第一天和最后一天
    var currentDay = new Date();
    var first  = this.formatDate(new Date(currentDay.getFullYear(), currentDay.getMonth(),1), 'yyyy-MM-dd');
    // var end  = this.formatDate(new Date(currentDay.getFullYear(), currentDay.getMonth() + 1, 0), 'yyyy-MM-dd');
    var end  = this.formatDate(new Date(), 'yyyy-MM-dd');
    return this.getDays(first, end, formatStr)
  },
  //获取指定月份的每个日期
  getMonthDays(monthStr,formatStr = 'MM-dd') {
    // 获取当月的第一天和最后一天
    if(monthStr.indexOf('-') > -1){
      const monthSplits = monthStr.split('-');
      const year = monthSplits[0]
      const month = monthSplits[1]
      return this.getDays(new Date(year, month, 1), new Date(year, month+1, 0), formatStr)
    }else{
      const year = monthStr.substring(0,4)
      const month = monthStr.substring(5)
      return this.getDays(new Date(year, month, 1), new Date(year, month+1, 0), formatStr)
    }
  },
  // 获取日期范围内每个日期
  // startDate 2022-03-26
  // endDate 2022-04-26
  // 返回：[2022-03-26, 2022-03-27, 2022-03-28]
  getDays(startDate, endDate, formatStr = 'MM-dd') {
    var result = new Array();
    var ab = startDate.split("-")
    var ae = endDate.split("-")
    var db = new Date()
    db.setUTCFullYear(ab[0], ab[1]-1, ab[2])
    var de = new Date()
    de.setUTCFullYear(ae[0], ae[1]-1, ae[2])
    var unixDb=db.getTime()
    var unixDe=de.getTime()
    for(var k=unixDb;k<=unixDe;){
      result.push(this.formatDate(new Date(parseInt(k)), formatStr))
      k=k+24*60*60*1000
    }
    return result
  },

  // 去重并相加
  /*  delSameObjValue 数组对象相同值相加去重
    arr 需要处理的数组
    resultNum 最终计算结果的键名
    keyName 用于计算判断的键名 
    keyValue 用于计算结果的键名 --> 对应的键值为number类型*/
  delSameObjValue(arr, resultNum, keyName, keyValue) {
    const warp = new Map();
    arr.forEach(i => {
        let str = keyName.map(v => i[v]).join('_');
        i[resultNum] = keyValue.reduce((p, c) => p += i[c], 0);
        warp.has(str) ? warp.get(str)[resultNum] += i[resultNum] : warp.set(str, i);
    });
    return Array.from(warp).map(([, v]) => v);
  },
  // 去重并保留最后一个元素
  // arr:需要去重的数组
  // name:根据去重的key
  uniqueLastChild(arr, name) {
    let hash={}
    return arr.reduce(function (acc, cru,index) {
      if (!hash[cru[name]]) {
        hash[cru[name]]={index:acc.length}  //就改了这一行代码，把保存的索引变成了当前acc的数组长度，很好的解决了问题
        acc.push(cru)
      }else{
        acc.splice(hash[cru[name]]['index'],1,cru)
      }
      return acc;
    }, []);
  },
}
export default skCommon